package cn.tedu.cart.service.impl;

import cn.tedu.cart.mapper.CartMapper;
import cn.tedu.cart.service.CartService;
import cn.tedu.pojo.param.CartAddParam;
import cn.tedu.pojo.po.CartPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public Boolean addCart(CartAddParam cartAddParam) {
        CartPO cartPO = new CartPO();
        BeanUtils.copyProperties(cartAddParam, cartPO);
        int result = cartMapper.insert(cartPO);
        return result == 1;
    }

    @Override
    public Boolean deleteCart(String productCode, String userId) {
        //delete from cart_tbl  where product_code=#{productCode} and user_id=#{userId}
        //需要为delete  update select拼接where条件，使用QueryWrapper工具完成
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_code", productCode);
        queryWrapper.eq("user_id", userId);
        int deleteRows = cartMapper.delete(queryWrapper);
        return deleteRows >= 1;
    }
}
